package com.bt.lms.ui.page.stu;

import com.alibaba.fastjson.JSON;
import com.bt.lms.constants.RegStuBizTypeConstant;
import com.bt.lms.entity.RegStuInfo;
import com.bt.lms.entity.page.Page;

import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.ui.page.common.action.pageenp.ApplyStuLabAction;
import com.bt.lms.ui.page.sys.style.BtnStyle;
import com.bt.lms.ui.page.sys.style.LabStyle;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.ui.vo.regstu.ApplyStuInfo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * 报名学员管理界面
 */
public class PageMainStudentList extends VBox {


    //界面有关的
    //查询
    /**
     * 报名学员管理界面宽度
     */
    private double preWidth = 1280;

    /**
     * 报名学员管理界面高度
     */
    private double preHeight = 830;

    /**
     * 招生计划条件
     */
    private ComboBox<String> enpTypeCB;

    /**
     * 模糊搜索条件
     */
    private TextField vagueSearchTF;

    /**
     * 学习中心条件
     */
    private TextField learningCenTF;

    /**
     * 报名方式条件
     */
    private ComboBox<String> regWayCB;

    /**
     * 邮箱状态条件
     */
    private ComboBox<String> emailStatusCB;

    /**
     * 录取状态条件
     */
    private ComboBox<String> enrollStatusCB;

    /**
     * 学员编号姓名条件
     */
    private TextField stuCodeOrNameTF;


    /**
     * 列表组件
     */
    private TableView listTableView;


    //----------事件

    /**
     * 新增报名学员事件
     */
    private AddStuAction addStuAction;

    /**
     * 分页查询事件
     */
    private SearchAction pageAction;


    //-----数据

    /**
     * 列表的数据
     */

    private List tableData;

    /**
     * 查询条件
     */
    private SearchVO searchVO;


    /**
     * 当前页码数
     */

    private Integer currentPageNo = 1;

    /**
     * 每页显示多少条  默认10条
     */

    private Integer pageSize = 10;


    /**
     * 列名默认长度
     */
    private double columnPreWidth = 100;

    /**
     * 界面跳转信息
     */
    private PageSkipInfo pageSkipInfo;




    /**
     * 构造主界面
     */
    public PageMainStudentList() {

        this.pageSkipInfo = pageSkipInfo;
        this.setPadding(new Insets(5));
        this.setSpacing(20);
        this.setPrefSize(preWidth, preHeight);


        //初始化界面
        initUi();

        //加载数据并渲染分页
        loadAndRenderingPage(currentPageNo);

    }

    /**
     * 加载数据 并从渲染分页
     *
     * @param currentPageNo 当前页
     */
    private void loadAndRenderingPage(Integer currentPageNo) {

        Page objectPageQuery = new Page();
        objectPageQuery.setCurrent(currentPageNo);
        objectPageQuery.setSize(this.pageSize);

        //分页条件查询
        Page pageQuery = query(objectPageQuery);


    }

    /**
     * 分页条件查询
     *
     * @param pageQuery 分页查询
     * @return
     */
    private Page query(Page pageQuery) {


        //请求消息
        ReqMsg reqMsg = new ReqMsg();
        //业务类型
        reqMsg.setBizType(RegStuBizTypeConstant.STU_REG_INFO_PAGING_QUERY);
        String pageStr = JSON.toJSONString(pageQuery);
        //数据带走
        reqMsg.setData(pageStr);
        byte[] bytes = reqMsg.toBytes();

        //组网
        String ip = "127.0.0.1";
        Integer port = 8888;
        Socket socket = null;
        try {
            socket = new Socket(ip, port);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //发送消息
        try {
            OutputStream out = socket.getOutputStream();
            out.write(bytes);
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        //读取响应消息
        try {
            InputStream in = socket.getInputStream();
            byte[] buffer = new byte[2048];
            in.read(buffer);

            RespMsg respMsg = RespMsg.toRespMsg(buffer);
            String data = respMsg.getData();

            Page pageQuery1 = JSON.parseObject(data, Page.class);
            List records = pageQuery1.getRecords();

            //报名学员信息集合
            List<RegStuInfo> regStuInfos = new ArrayList<>();
            for (Object obj : records) {
                RegStuInfo regStuInfo = JSON.parseObject(obj.toString(), RegStuInfo.class);
                regStuInfos.add(regStuInfo);


            }

            List<ApplyStuInfo> applyStuInfos = new ArrayList<>();

            for (RegStuInfo regStuInfo : regStuInfos) {

                //报名编号
                String registrationCode = regStuInfo.getRegistrationCode();
                //姓名
                String name = regStuInfo.getName();
                //性别
                Integer gender = regStuInfo.getGender();
                String genderStr = null;
                switch (gender) {
                    case 1:
                        genderStr = "男";
                        break;
                    case 2:
                        genderStr = "女";
                        break;
                }
                //报名方式
                Integer reqWay = regStuInfo.getReqWay();
                String reqWayStr = null;
                switch (reqWay) {
                    case 1:
                        reqWayStr = "在线报名";
                        break;
                    case 2:
                        reqWayStr = "线下报名";
                        break;
                }
                Integer regMaterialStatus = regStuInfo.getRegMaterialStatus();
                String regMaterialStatusStr = null;
                switch (regMaterialStatus) {
                    case 1:
                        regMaterialStatusStr = "已完整提交";
                        break;
                    case 2:
                        regMaterialStatusStr = "未完整提交";
                        break;
                }
                //邮箱状态
                Integer emailStatus = regStuInfo.getEmailStatus();
                String emailStatusStr = null;
                switch (emailStatus) {
                    case 1:
                        emailStatusStr = "已验证";
                        break;
                    case 2:
                        emailStatusStr = "未验证";
                        break;
                }


                HBox operationHBox = new HBox();


                ApplyStuInfo applyStuInfo = new ApplyStuInfo(registrationCode, name, genderStr, reqWayStr, "", regMaterialStatusStr, emailStatusStr, "", "", "",operationHBox);

                applyStuInfos.add(applyStuInfo);
            }


            //把获取到的信息 展现到列表里
            ObservableList<ApplyStuInfo> applyStuInfos1 = FXCollections.observableArrayList(applyStuInfos);
            this.listTableView.setItems(applyStuInfos1);


        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return pageQuery;
    }


    /**
     * 初始化界面
     */
    private void initUi() {


        //顶部横向盒子
        HBox topHBox = new HBox();
        topHBox.setSpacing(10);
        topHBox.setPadding(new Insets(10));
        //标签
        Label topLab = new Label("当前位置:🏠>招生管理>报名学员管理");
        topLab.setFont(new Font(20));
        //顶部盒子带走
        topHBox.getChildren().addAll(topLab);

        //搜索区域横向盒子
        HBox searchHBox = new HBox();
        searchHBox.setPadding(new Insets(10));
        searchHBox.setSpacing(20);
        //招生条件
        this.enpTypeCB = new ComboBox<>();
        this.enpTypeCB.getItems().add("按招生计划");
        this.enpTypeCB.getItems().add("按招生院校");
        this.enpTypeCB.getItems().add("按招生");
        this.enpTypeCB.getItems().add("按招生层次");
        this.enpTypeCB.getItems().add("按招生专业");
        this.enpTypeCB.getSelectionModel().selectFirst();
        //模糊搜索标签
        Label vagueSearchLab = new Label("模糊搜索");
        vagueSearchLab.setFont(new Font(20));//设置字体大小20
        //模糊搜索文本框
        this.vagueSearchTF = new TextField();
        //选择学习中心标签
        Label learningLab = new Label("选择学习中心");
        learningLab.setFont(new Font(20));//设置字体大小20
        //学习中心文本框
        this.learningCenTF = new TextField();

        //搜索区域横向盒子带走
        searchHBox.getChildren().addAll(this.enpTypeCB, vagueSearchLab, this.vagueSearchTF, learningLab, this.learningCenTF);

        //更多搜索区域横向盒子
        HBox moreSearchHBox = new HBox();
        moreSearchHBox.setSpacing(20);
        moreSearchHBox.setPadding(new Insets(10));

        //更多搜索标签
        Label moreSearchLab = new Label("更多搜索→");
        moreSearchLab.setFont(new Font(20));
        moreSearchLab.setTextFill(Color.rgb(0, 143, 191));

        //报名方式类型下拉框
        this.regWayCB = new ComboBox<>();
        this.regWayCB.getItems().add("报名方式");
        this.regWayCB.getItems().add("在线报名");
        this.regWayCB.getItems().add("线下报名");
        this.regWayCB.getSelectionModel().selectFirst();

        //邮箱状态类型下拉框
        this.emailStatusCB = new ComboBox<>();
        this.emailStatusCB.getItems().add("邮箱状态");
        this.emailStatusCB.getItems().add("已验证");
        this.emailStatusCB.getItems().add("未验证");
        this.emailStatusCB.getSelectionModel().selectFirst();//有默认值

        //录取状态下拉框
        this.enrollStatusCB = new ComboBox<>();
        this.enrollStatusCB.getItems().add("录取状态");
        this.enrollStatusCB.getItems().add("待录取");
        this.enrollStatusCB.getItems().add("已录取");
        this.enrollStatusCB.getItems().add("未录取");
        this.enrollStatusCB.getSelectionModel().selectFirst();

        //学员编号姓名文本
        Label stuNodeOrNameLab = new Label("学员编号/姓名");
        stuNodeOrNameLab.setFont(new Font(20));
        //文本框
        this.stuCodeOrNameTF = new TextField();
        //搜索按钮
        Button searchBtn = new Button("搜索🔍");
        BtnStyle.cancelBtn(searchBtn);
        searchBtn.setCursor(Cursor.HAND);


        //更多搜索盒子带走
        moreSearchHBox.getChildren().addAll(moreSearchLab, this.regWayCB, this.emailStatusCB, this.enrollStatusCB, stuNodeOrNameLab, this.stuCodeOrNameTF, searchBtn);

        //新增报名学员盒子
        HBox addHBox = new HBox();
        addHBox.setPadding(new Insets(10));
        //新增报名学员按钮
        Button addStuBtn = new Button("新增报名学员");
        BtnStyle.addBtn(addStuBtn);
        this.addStuAction = new AddStuAction();
        addStuBtn.setOnAction(this.addStuAction);
        this.addStuAction.setPageMainStudentList(this);
        addStuBtn.setCursor(Cursor.HAND);

        //带走
        addHBox.getChildren().addAll(addStuBtn);

        //列表盒子
        VBox tableVBox = new VBox();
        //列表
        this.listTableView = new TableView<>();

        //列名
        TableColumn regCodeColumn = new TableColumn<>("报名编号");
        TableColumn nameColumn = new TableColumn<>("姓名");
        TableColumn genderColumn = new TableColumn<>("性别");
        TableColumn regWayColumn = new TableColumn<>("报名方式");
        TableColumn enpNameColumn = new TableColumn<>("所属招生计划");
        enpNameColumn.setPrefWidth(150);
        TableColumn learningCenColumn = new TableColumn<>("所属学习中心");
        learningCenColumn.setPrefWidth(150);
        TableColumn reqMaterialColumn = new TableColumn<>("报名材料");
        TableColumn emailStatusColumn = new TableColumn<>("邮箱状态");
        TableColumn accountColumn = new TableColumn<>("帐号");
        TableColumn enrollStatusColumn = new TableColumn<>("录取状态");
        TableColumn operationColumn = new TableColumn<>("操作");
        operationColumn.setPrefWidth(250);

        //映射到表格中
        regCodeColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("reqCode"));
        nameColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("name"));
        genderColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("gender"));
        regWayColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("reqWay"));
        enpNameColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("enpName"));
        learningCenColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("learningCen"));
        reqMaterialColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("reqMaterial"));
        emailStatusColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("emailStatus"));
        accountColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("account"));
        enrollStatusColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("enrollStatus"));
        operationColumn.setCellValueFactory(new PropertyValueFactory<ApplyStuInfo, String>("operation"));
        listTableView.getColumns().addAll(regCodeColumn, nameColumn, genderColumn, regWayColumn, enpNameColumn, learningCenColumn, reqMaterialColumn, emailStatusColumn, accountColumn, enrollStatusColumn, operationColumn);

        //带走
        tableVBox.getChildren().addAll(this.listTableView);

        //页码横向盒子
        HBox pageNoHBox = new HBox();
        pageNoHBox.setPadding(new Insets(10));
        pageNoHBox.setAlignment(Pos.CENTER_RIGHT);
        pageNoHBox.setSpacing(10);
        //首页标签
        Label homePage = new Label("<<首页");
        LabStyle.paging(homePage);
        homePage.setCursor(Cursor.HAND);
        //上一页标签
        Label upPageLab = new Label("<<上一页");
        LabStyle.paging(upPageLab);
        upPageLab.setCursor(Cursor.HAND);
        //1按钮
        Button btn1 = new Button("1");
        BtnStyle.pagingCurrent(btn1);
        btn1.setCursor(Cursor.HAND);
        //2按钮
        Button btn2 = new Button("2");
        BtnStyle.paging(btn2);
        btn2.setCursor(Cursor.HAND);
        //3按钮
        Button btn3 = new Button("3");
        BtnStyle.paging(btn3);
        btn3.setCursor(Cursor.HAND);
        //4按钮
        Button btn4 = new Button("4");
        BtnStyle.paging(btn4);
        btn4.setCursor(Cursor.HAND);
        //下一页标签
        Label nextLab = new Label("下一页");
        LabStyle.paging(nextLab);
        nextLab.setCursor(Cursor.HAND);
        //尾页标签
        Label lastLab = new Label("尾页");
        LabStyle.paging(lastLab);
        lastLab.setCursor(Cursor.HAND);
        //条数标签
        Label totalLab = new Label("总记录数");
        LabStyle.paging(totalLab);

        //100标签
        Label sumLab = new Label("100");
        LabStyle.status(sumLab, LabStyle.STOP);
        //条
        Label pageNoLab = new Label("条");

        //跳转框
        TextField pageNoTF = new TextField();
        pageNoTF.setPrefWidth(50);
        //10标签
        Label tenLab = new Label("/10");

        //go按钮
        Button goBtn = new Button("go");
        goBtn.setCursor(Cursor.HAND);

        //带走
        pageNoHBox.getChildren().addAll(homePage, upPageLab, btn1, btn2, btn3, btn4, nextLab, lastLab, totalLab, sumLab, pageNoLab, pageNoTF, tenLab, goBtn);


        //主盒子带走
        this.getChildren().addAll(topHBox, searchHBox, moreSearchHBox, addHBox, tableVBox, pageNoHBox);


    }

    /**
     * 添加报名学员事件
     */
    private class AddStuAction implements EventHandler {
        /**
         * 列表页
         */
        private PageMainStudentList pageMainStudentList;

        @Override
        public void handle(Event event) {


            //添加报名学员界面
            PageAddStuInfo pageAddStuInfo = new PageAddStuInfo();
            Pane pane = ApplyStuLabAction.pageSkipInfo.getPane();
            Integer index = ApplyStuLabAction.pageSkipInfo.getIndex();
            pane.getChildren().set(index, pageAddStuInfo);

        }


        public PageMainStudentList getPageMainStudentList() {
            return pageMainStudentList;
        }

        public void setPageMainStudentList(PageMainStudentList pageMainStudentList) {
            this.pageMainStudentList = pageMainStudentList;
        }
    }

    /**
     * 分页查询事件
     */
    private class SearchAction implements EventHandler {

        /**
         * 列表界面
         */
        private PageMainStudentList pageMainStudentList;

        @Override
        public void handle(Event event) {

            //页码
            Button btn = (Button) event.getSource();
            //获取页码
            String text = btn.getText();

            //如果是数字的话
            if (text.matches("^[1-9]\\d*")) {
                int i = Integer.parseInt(text);
                this.pageMainStudentList.setCurrentPageNo(i);
            } else {
                //如果是文本的话
                if ("搜索".equals(text)) {
                    this.pageMainStudentList.currentPageNo = 1;
                } else if ("上一页".equals(text)) {
                    this.pageMainStudentList.currentPageNo
                            = this.pageMainStudentList.currentPageNo == 1 ? 1 : this.pageMainStudentList.currentPageNo - 1;

                }
            }
            //加载数据
            this.pageMainStudentList.loadAndRenderingPage(currentPageNo);

        }

        public PageMainStudentList getPageMainStudentList() {
            return pageMainStudentList;
        }

        public void setPageMainStudentList(PageMainStudentList pageMainStudentList) {
            this.pageMainStudentList = pageMainStudentList;
        }
    }

    /**
     * 查询条件信息
     */
    class SearchVO {

        /**
         * 招生计划类型
         */
        private Integer enpType;

        /**
         * 模糊搜索
         */
        private String vagueSearch;

        /**
         * 学习中心
         */
        private String learningCen;

        /**
         * 报名方式类型
         */
        private Integer reqType;

        /**
         * 邮箱状态类型
         */
        private Integer emailStatusType;

        /**
         * 录取状态类型
         */
        private Integer enrollStatusType;

        /**
         * 学员编号 姓名
         */
        private String stuCodeOrName;


        public Integer getEnpType() {
            return enpType;
        }

        public void setEnpType(Integer enpType) {
            this.enpType = enpType;
        }

        public String getVagueSearch() {
            return vagueSearch;
        }

        public void setVagueSearch(String vagueSearch) {
            this.vagueSearch = vagueSearch;
        }

        public String getLearningCen() {
            return learningCen;
        }

        public void setLearningCen(String learningCen) {
            this.learningCen = learningCen;
        }

        public Integer getReqType() {
            return reqType;
        }

        public void setReqType(Integer reqType) {
            this.reqType = reqType;
        }

        public Integer getEmailStatusType() {
            return emailStatusType;
        }

        public void setEmailStatusType(Integer emailStatusType) {
            this.emailStatusType = emailStatusType;
        }

        public Integer getEnrollStatusType() {
            return enrollStatusType;
        }

        public void setEnrollStatusType(Integer enrollStatusType) {
            this.enrollStatusType = enrollStatusType;
        }

        public String getStuCodeOrName() {
            return stuCodeOrName;
        }

        public void setStuCodeOrName(String stuCodeOrName) {
            this.stuCodeOrName = stuCodeOrName;
        }
    }


    public ComboBox<String> getEnpType() {
        return enpTypeCB;
    }

    public void setEnpType(ComboBox<String> enpType) {
        this.enpTypeCB = enpType;
    }

    public TextField getVagueSearchTF() {
        return vagueSearchTF;
    }

    public void setVagueSearchTF(TextField vagueSearchTF) {
        this.vagueSearchTF = vagueSearchTF;
    }

    public TextField getLearningCenTF() {
        return learningCenTF;
    }

    public void setLearningCenTF(TextField learningCenTF) {
        this.learningCenTF = learningCenTF;
    }

    public ComboBox<String> getRegWayCB() {
        return regWayCB;
    }

    public void setRegWayCB(ComboBox<String> regWayCB) {
        this.regWayCB = regWayCB;
    }

    public ComboBox<String> getEmailStatus() {
        return emailStatusCB;
    }

    public void setEmailStatus(ComboBox<String> emailStatus) {
        this.emailStatusCB = emailStatus;
    }

    public ComboBox<String> getEnrollStatus() {
        return enrollStatusCB;
    }

    public void setEnrollStatus(ComboBox<String> enrollStatus) {
        this.enrollStatusCB = enrollStatus;
    }

    public TextField getStuCodeOrNameTF() {
        return stuCodeOrNameTF;
    }

    public void setStuCodeOrNameTF(TextField stuCodeOrNameTF) {
        this.stuCodeOrNameTF = stuCodeOrNameTF;
    }

    public TableView getListTableView() {
        return listTableView;
    }

    public void setListTableView(TableView listTableView) {
        this.listTableView = listTableView;
    }

    public AddStuAction getAddStuAction() {
        return addStuAction;
    }

    public void setAddStuAction(AddStuAction addStuAction) {
        this.addStuAction = addStuAction;
    }

    public SearchAction getPageAction() {
        return pageAction;
    }

    public void setPageAction(SearchAction pageAction) {
        this.pageAction = pageAction;
    }

    public Integer getCurrentPageNo() {
        return currentPageNo;
    }

    public void setCurrentPageNo(Integer currentPageNo) {
        this.currentPageNo = currentPageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
}
